package com.du.web; /**
 * @author : DuJiabao
 * @Date : 2021/1/28 21:40
 * @Project : bookstore
 * @File : ${FILE_NAME}
 * @Desc :
 */

import com.du.pojo.User;
import com.du.service.UserService;
import com.du.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repwd = request.getParameter("repwd");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        if (!code.equalsIgnoreCase("abc")) {
            System.out.println("验证码错误");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            return;
        }

        if (userService.existsUser(username)) {
            System.out.println(username + " 该用户名已存在");
            request.setAttribute("msg", "该用户名已存在");
            request.setAttribute("username", username);
            request.setAttribute("password", password);
            request.setAttribute("repwd", repwd);
            request.setAttribute("email", email);
            request.setAttribute("code", code);
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            return;
        }

        userService.registerUser(new User(0, username, password, email));
        System.out.println(username + " 注册用户成功");
        request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request, response);
    }
}
